﻿using System.Security.Claims;
using System.Threading.Tasks;
using IdentityServer4.Models;
using IdentityServer4.Validation;

namespace IdentityServer4.IdServerV2
{
    public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
    {
        public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
        {
            //根据context.UserName和context.Password与数据库的数据做校验，判断是否合法
            if (context.UserName == "ClueeZhuo" && context.Password == "123654")
            {
                context.Result = new GrantValidationResult(
                 subject: context.UserName,
                 authenticationMethod: "custom",
                 claims: new Claim[] {
                     new Claim("Name", context.UserName),
                     new Claim("UserId", "1639"),
                     new Claim("RealName", "卓克路"),
                     new Claim("Email", "clueezhuo@outlook.com"),
                     new Claim("Nick", "Banjo") });
            }
            else
            {
                //验证失败
                context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "invalid custom credential");
            }
        }
    }
}
